<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>模板编辑器</title>
    <#assign base=request.contextPath  />
    <link rel="stylesheet" href="${base}/css/index.css">
    <script type="text/javascript" charset="utf-8" src="${base}/ueditor/ueditor.config.js"></script>
    <script type="text/javascript" charset="utf-8" src="${base}/ueditor/ueditor.all.min.js"> </script>
    <script type="text/javascript" charset="utf-8" src="${base}/ueditor/lang/zh-cn/zh-cn.js"></script>
    <script type="text/javascript" charset="utf-8" src="${base}/js/index.js"></script>

    <style type="text/css">
        div{
            width:100%;
        }
    </style>
</head>
<body>
<input type="hidden" id="iUserId" name="iUserId" value="${userId}">
<input type="hidden" id="iTitle" name="iTitle" value="${title}">
<input type="hidden" id="iBody" name="iBody" value="${body}">
<input type="hidden" id="iCreatDate" name="iCreatDate" value="${creatDate}">
<input type="hidden" id="iModifyDate" name="iModifyDate" value="${modifyDate}">
<div>
    <hr>
    <strong>标题:</strong><input type="text" name="title" id="title" value="${title}">
    <script id="editor" type="text/plain" style="width:1024px;height:500px;"></script>
</div>
<div>
    <button onclick="submitText()">提交</button>
</div>
<script src="${base}/js/jquery.min.js"></script>
<script src="${base}/js/jquery.base64.js"></script>
<script type="text/javascript">
    $.base64.utf8encode = true;
    // console.log($.base64.atob(bodytext,true));

    $(function () {
        var ue = UE.getEditor('editor');
        var bodytext = $("#iBody").val();
        ue.ready(function() {
            UE.getEditor('editor').setHeight(500);
            UE.getEditor('editor').setContent($.base64.atob(bodytext,true));
        });
        function isFocus(e){
            alert(UE.getEditor('editor').isFocus());
            UE.dom.domUtils.preventDefault(e)
        }
        function setblur(e){
            UE.getEditor('editor').blur();
            UE.dom.domUtils.preventDefault(e)
        }
    });

    function submitText() {
        if (!UE.getEditor('editor').hasContents()) {
            alert("内容为空");
            return;
        }
        var title = $("#title").val();
        // var b = new Base64();
        var str = $.base64.btoa(UE.getEditor('editor').getContent());
        console.log("yuanstr:"+UE.getEditor('editor').getContent());
        console.log("str:"+str);
        $.ajax({
            url: "/texts/submit",
            type: "POST",
            async: false,
            data: {
                title: title,
                body: str
            },
            success: function (data) {
                if (data == "SUCCESS") {
                    alert("提交成功！")
                }else{
                    alert("提交失败："+data);
                }
            },
            error: function (data) {
                alert("服务请求异常")
            }
        });
    }
</script>
</body>
</html>